package org.tokaf.datasearcher;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;

import org.tokaf.normalizer.SimpleNormalizer;

/**
 * <p>Searches for triples in CVS file. This datasearcher is made only for
 * testing the top-k algorithms.</p> <p>Copyright (c) 2006</p>
 * @author Alan Eckhardt
 * @version 1.0
 */
public class CVSSearcher extends DataSearcher {

	String actualLine;

	BufferedReader reader = null;

	FileReader fileReader = null;

	int numberOfColumn = -1;

	int columnCount = -1;

	int line = -1;

	String fileName;

	public CVSSearcher(int column, int columnCount) {
		this.numberOfColumn = column;
		this.columnCount = columnCount;
	}

	public boolean needsSort() {
		return false;
	}

	public SimpleNormalizer getDefaultNormalizer() {
		return new SimpleNormalizer();
	}

	public void sort(int index) throws UnsupportedOperationException {
		throw new UnsupportedOperationException();
	}

	public void closeFile() {
		try {
			if (fileReader != null) {
				fileReader.close();
				fileReader = null;
			}
			if (reader != null) {
				reader.close();
				reader = null;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	protected void finalize() throws Throwable {
		super.finalize();
		closeFile();
	}

	public boolean hasNext() {
		if (line < 0 || actualLine == null)
			return false;
		return true;
	}

	public Object getField(int index) {
		int i = 0;
		int actualColumn = 0;
		while (actualColumn < index && i < actualLine.length()) {
			i += actualLine.substring(i, actualLine.length()).indexOf(";");
			i++;
			actualColumn++;
		}
		if (actualLine == null)
			return null;
		if (actualLine.substring(i + 1, actualLine.length()).equals(""))
			return null;

		int j = actualLine.substring(i + 1, actualLine.length()).indexOf(";");
		String field = actualLine.substring(i, i + j + 1);
		return field;
	}

	public Object getFieldAtPosition(int index, int position) {
		throw new UnsupportedOperationException();
	}

	public int advance() {
		if (reader == null)
			return -1;
		int i = 0;
		int actualColumn = 0;
		try {
			actualLine = reader.readLine();
			if (actualLine == null)
				return -1;
		} catch (IOException e) {
			e.printStackTrace();
			return -1;
		}
		while (actualColumn < this.numberOfColumn * this.columnCount
				&& i < actualLine.length()) {
			i = actualLine.indexOf(";");
			actualColumn++;
			actualLine = actualLine.substring(i + 1, actualLine.length());
		}
		line++;
		return 0;
	}

	public void initSearch(String query) {
		long startTime = System.nanoTime();
		advance();
		initSearchTime += System.nanoTime() - startTime;
	}

	public int initSource(String db) {

		long startTime = System.nanoTime();
		try {
			fileName = db;
			fileReader = new FileReader(db);
			reader = new BufferedReader(fileReader, 1024);
			line = 0;
		} catch (Exception ex) {
			initSourceTime += System.nanoTime() - startTime;
		}
		initSourceTime += System.nanoTime() - startTime;

		return 1;
	}

	public int initSource(FileReader db, String filename) {

		long startTime = System.nanoTime();
		try {
			fileName = filename;
			reader = new BufferedReader(db, 1024);
			line = 0;
		} catch (Exception ex) {
			initSourceTime += System.nanoTime() - startTime;
		}
		initSourceTime += System.nanoTime() - startTime;

		return 1;
	}

	public int initSource(DataSearcher searcher) {
		long startTime = System.nanoTime();

		if (searcher instanceof CVSSearcher) {
			initSource(((CVSSearcher) searcher).fileReader,
					((CVSSearcher) searcher).fileName);
			// reader=((CVSSearcher)searcher).reader;
			try {
				reader.mark(10240);
			} catch (IOException e) {
				initSourceTime += System.nanoTime() - startTime;
			}
			initSourceTime += System.nanoTime() - startTime;
			return 0;
		}
		initSourceTime += System.nanoTime() - startTime;
		return -1;
	}

	public Object findObject(Object obj, int position, int index)
			throws UnsupportedOperationException {
		throw new UnsupportedOperationException();
	}

	public int getPosistion() {
		return line;
	}

	public void resetSearch() {
		try {
			reader.reset();
			line = 0;
		} catch (IOException e) {
			initSource(fileName);
			line = 0;
		}
	}

	public Object clone() {
		CVSSearcher newInstance = new CVSSearcher(numberOfColumn, columnCount);
		newInstance.initSource(this);
		return newInstance;
	}

	public Object next() {
		Vector v = new Vector();
		;
		for (int i = 0; i < columnCount; i++) {
			v.add(getField(i));
		}
		advance();
		return v;
	}

}
